package com.bubaiwantong.algorithms2.insert;

public class ShellSort {


    public static void main(String[] args) {
        ShellSort shellSort = new ShellSort();
//        int[] arr = new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        int[] arr = new int[]{9, 8, 7, 6, 0, 5, 4, 3, 2, 1};
        shellSort.printArr(arr);
        shellSort.sort(arr);
        shellSort.printArr(arr);
    }

    /**
     * 简单插入排序
     * 选择一个合适的位置插入
     *
     * @param arr
     */
    public void sort(int[] arr) {
        for (int gap = 4; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i += gap) {
                int temp = arr[i];
                int j = i;
                while (j - gap >= 0 && temp < arr[j - gap]) {
                    arr[j] = arr[j - gap];
                    j -= gap;
                }
                arr[j] = temp;
            }
        }
    }

    public void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
